/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security.controller;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.vacoor.nothing.common.util.StringUtils;
import org.vacoor.nothing.security.entity.Role;
import org.vacoor.nothing.security.entity.User;
import org.vacoor.nothing.security.service.UserService;
import org.vacoor.nothing.web.domain.Filters;
import org.vacoor.nothing.web.domain.Page;
import org.vacoor.nothing.web.domain.Pageable;

import java.util.List;
import java.util.Map;

//import javax.validation.Valid;

/**
 * @author vacoor
 */
@Controller
@RequestMapping("/users")
public class UserController {
    //    @Autowired
    private UserService userService;


    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("test")
    public String test(Model model, Map<String, Object> map) {
        model.addAttribute("root", "ok");
        return "forward:root";
    }

    @ResponseBody
    @RequestMapping("create")
    public Map<String, Object> create(User user, BindingResult bindingResult, @RequestParam("roleIds") String[] roleIds) {
        fillUserRoles(user, roleIds);
        userService.save(user);
        user = userService.find(user.getId());
        return ImmutableMap.<String, Object>of("success", true);
    }

    @ResponseBody
    @RequestMapping("list")
    public Page<User> list(Filters filters, Pageable pageable) {
        System.out.println("list2");
        return userService.findPage(filters, pageable);
    }


    @ResponseBody
    @RequestMapping("update")
    public Map<String, Object> update(User user, @RequestParam("roleIds") String[] roleIds) {
        fillUserRoles(user, roleIds);
        userService.update(user);
        user = userService.find(user.getId());

        return ImmutableMap.<String, Object>of("success", true);
    }

    @ModelAttribute
    public User bindModel(@RequestParam(value = "id", required = false) String id) {
        return !StringUtils.hasLength(id) ? new User() : userService.find(id);
    }

    private void fillUserRoles(User user, String[] roleIds) {
        if (user == null || roleIds == null || roleIds.length < 1) {
            return;
        }

        List<Role> roles = Lists.newArrayList();
        for (String id : roleIds) {
            Role r = new Role();
            r.setId(id);
            roles.add(r);
        }

        user.setRoles(roles);
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // 不允许绑定 roles 手动绑定
        binder.setDisallowedFields("roles");
    }

}
